using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace AMS.Inventory.PDA.Castle.DataAccess
{
    public partial class UsrCotrlComBox : UserControl
    {
        private ListBox.ObjectCollection _arrayCheck;
        private string _strFileFullName;
        private DataTable _typeDt;
        public SqliteDBServer SqlDb;
        public string StrClassName;
        public string StrTypeName;

        public UsrCotrlComBox()
        {
            InitializeComponent();
        }

        public UsrCotrlComBox(string strClassName, string strTypeName, SqliteDBServer sqlDb)
        {
            InitializeComponent();
            StrClassName = strClassName;
            StrTypeName = strTypeName;
            SqlDb = sqlDb;
            _strFileFullName = ModCommon.PrjPath + "\\BaseData\\" + StrClassName + "_" + StrTypeName + ".xml";
            ReadXml();
        }

        private void BtnReviewClick(object sender, EventArgs e)
        {
            var frmTypename = new FrmTypename(_typeDt, StrClassName, StrTypeName, SqlDb);
            frmTypename.ShowDialog();
            _arrayCheck = frmTypename._arrayCheck;
            string containStr = "";
            foreach (object list in _arrayCheck)
            {
                containStr += "'" + list + "',";
            }
            WriteXml(containStr);
            ReadXml();
        }

        private void ReadXml()
        {
            _typeDt = new DataTable(StrClassName + StrTypeName);
            if (File.Exists(_strFileFullName))
                _typeDt.ReadXml(_strFileFullName);
            cboType.DataSource = _typeDt;
            if (_typeDt.Rows.Count > 0)
                cboType.DisplayMember = _typeDt.Columns[0].ToString();
        }

        private void WriteXml(string containStr)
        {
            DataTable typeDt = GetTableByCon(containStr);
            string strDir = ModCommon.PrjPath + "\\BaseData";
            if (!Directory.Exists(strDir))
                Directory.CreateDirectory(strDir);
            _strFileFullName = strDir + "\\" + StrClassName + "_" + StrTypeName + ".xml";
            if (File.Exists(_strFileFullName))
                File.Delete(_strFileFullName);
            typeDt.TableName = StrClassName + StrTypeName;
            typeDt.WriteXml(_strFileFullName, XmlWriteMode.WriteSchema);
        }

        private DataTable GetTableByCon(string containStr)
        {
            string sql;
            if (!string.IsNullOrEmpty(containStr))
            {
                sql = "SELECT keyVal,keyVal2,keyVal3 FROM AmsInventory_BaseDataInfo where ClassName='" + StrClassName +
                      "' and TypeName='" + StrTypeName + "' and KeyVal in( " +
                      containStr.Substring(0, containStr.Length - 1) + ")";
            }
            else
            {
                sql = "SELECT keyVal,keyVal2,keyVal3 FROM AmsInventory_BaseDataInfo where ClassName='" + StrClassName +
                      "' and TypeName='" + StrTypeName + "' and  1=2";
            }
            DataTable typeDt = SqlDb.GetDataTable(sql);
            return typeDt;
        }
    }
}
